/*
 * ====================================================================================
 * Copyright 2012 Eric Hartwell  (http://www.ehartwell.com)
 * ====================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable
 * law or agreed to in writing, software distributed under the License is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 * ====================================================================================
 */

package com.ehartwell.samples.android.pagerview;

import android.content.Context;
import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.view.MotionEvent;
import android.view.View;

/**
 * Fragment add-on interface to notify fragments when the primary fragment changes
 */
public class PagerFragment extends Fragment {

    /**
     * Called when this fragment becomes active (visible).
	 *
	 * @param context Context.
	 */
    public void onResumeFragment(Context context) { }

    /**
     * Called when this fragment becomes inactive (hidden).
	 *
	 * @param context Context.
     */
    public void onPauseFragment(Context context) { }

    /**
     * Gets the title string.
     *
     * @return Title string, or <tt>null</tt> if none available.
     */
    public String getTitle() {
        return null;
    }

    /**
     * Determines if the motion event should be handled inside this fragment, such as by a Gallery control.
     *
     * Usage: <tt>return isEventInsideView(motionEvent, view);</tt>
     *
     * @param motionEvent The motion event being dispatched down the hierarchy.
     * @return <tt>True</tt> if the motion event should be handled inside this fragment.
     */
    public boolean isLocalTouchEvent(MotionEvent motionEvent) {
        // return isEventInsideView(motionEvent, view);
        return false;
    }

    /**
     * Determines if a motion event occurred inside a given view.
     * @param motionEvent The motion event being dispatched down the hierarchy
     * @param view The view to test against.
     * @return <tt>True</tt> if a motion event occurred inside the view.
     */
    public static boolean isEventInsideView(MotionEvent motionEvent, View view) {
        // Get the area of the view in screen coordinates
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        Rect rect = new Rect();
        view.getLocalVisibleRect(rect);
        rect.offset(location[0], location[1]);

        // See if the event is within the view's area.
        return rect.contains((int)motionEvent.getRawX(), (int)motionEvent.getRawY());
    }
}
